package com.yun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yun.entity.Dorm;
import com.yun.entity.dto.UpdateDormByRoomNoDto;
import com.yun.mapper.DormMapper;
import com.yun.service.DormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyj
 * @since 2021-09-07
 */
@Service
public class DormServiceImpl extends ServiceImpl<DormMapper, Dorm> implements DormService {

    @Autowired
    private DormMapper dormMapper;

    @Override
    public List<Dorm> getAllDorm () {
        List<Dorm> dormList = dormMapper.selectList (null);
        return dormList;
    }

    @Override
    public CommonResult updateDorm (UpdateDormByRoomNoDto updateDormByRoomNoDto) {
        Dorm dorm = new Dorm ();
        dorm.setBedsNum (updateDormByRoomNoDto.getBedsNum ()).setDormType (updateDormByRoomNoDto.getDormType ()).setTel (updateDormByRoomNoDto.getTel ());
        int roomNo = dormMapper.update (dorm, new QueryWrapper<Dorm> ().eq ("roomNo", updateDormByRoomNoDto.getRoomNo ()));
        System.out.println (roomNo);
        if(roomNo > 0){
            return CommonResult.success ("更新成功");
        }
        return CommonResult.failed ("更新失败");
    }

    @Override
    public CommonResult insertDorm (Dorm dorm) {
        int insert = dormMapper.insert (dorm);
        if(insert > 0){
            return CommonResult.success ("插入成功");
        }
        return CommonResult.failed ("插入失败");
    }

    @Override
    public CommonResult deleteDormByRoomNo (String roomNo) {
        int i = dormMapper.delete (new QueryWrapper<Dorm> ().eq ("roomNo", roomNo));
        if (i > 0){
            return CommonResult.success ("删除成功");
        }
        return CommonResult.failed ("删除失败");
    }
}
